package com.xbjtech.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xbjtech.model.Store;
import com.xbjtech.model.StoreExample;
import com.xbjtech.model.StoreExample.Criteria;
import com.xbjtech.service.StoreService;

@Controller
@RequestMapping("/store")
public class StoreController {
	
	
	@Autowired
	StoreService storeService;
	
	
	@GetMapping("/getStoresByRegionId")
	@ResponseBody
	public List<Store> getStoresByRegionId(@RequestParam("regionId") Long regionId) {
		StoreExample example = new StoreExample();
		Criteria createCriteria = example.createCriteria();
		if(regionId.toString().matches("[0-9]{4}[0]{2}")) {
			int firstFourNumbers = (int)(regionId.intValue() / 100);
			// 市级单位  340100
			// select * from t_store where regionId like '3401__';
			createCriteria.andRegionIdLike(firstFourNumbers + "__");
		} else {
			// 区县级单位
			// select * from t_store where regionId = 340101
			createCriteria.andRegionIdEqualTo(regionId);
		}
		return storeService.getStoresByRegionId(example);
	}

}
